package com.collectplus.express.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;

import com.collectplus.express.MainActivity;

import java.util.List;

import droid.frame.App;

/**
 * Intent相关的跳转
 *
 * @author coffee<br>
 *         2016-11-17下午2:36:29
 */
public class Intents {

    /**
     * 到驿站、扫码核对包裹
     */
    public static String capture_station = "capture_station";

    /**
     * 拨号或者直接呼叫
     *
     * @param phone
     */
    public static void dialOrCall(String phone) {
        try {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + phone));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            App.getContext().startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Error e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送系统短信
     *
     * @param phone
     */
    public static void sendSMSMessage(String phone, String content) {
        try {
            Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("smsto:" + phone));
            intent.putExtra("sms_body", content);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            App.getContext().startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Error e) {
            e.printStackTrace();
        }
    }

    /**
     * 导航
     *
     * @parmam 前两个坐标是 百度的,后俩是高德的
     */
    public static void navigation(String latBd, String lngBd, String latGd, String lngGd, String address) {
        try {

            Context context = App.getContext();
            if (isInstallBaiduMap(context)) {
                Intent intent = new Intent();
                // 反向地址解析
                //intent.setData(Uri.parse("baidumap://map/geocoder?location=" + latBd + "," + lngBd));
                //自定义打点，且在指定坐标点上显示点的名称和内容信息
                intent.setData(Uri.parse("baidumap://map/marker?location=" + latBd + "," + lngBd + "&title=" + address));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } else if (isInstallGaodeMap(context)) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                // 将功能Scheme以URI的方式传入data
                Uri uri = Uri.parse("androidamap://navi?sourceApplication=" + context.getPackageName() + "&poiname=&" + //
                        "lat=" + latGd + "&lon=" + lngGd + "&dev=1&style=2");
                intent.setData(uri);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                // 启动该页面即可
                context.startActivity(intent);
            } else {
                String uriStr = "http://api.map.baidu.com/geocoder?location=" + latBd + "," + lngBd + //
                        "&output=html&src=" + context.getPackageName();
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriStr));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Error e) {
            e.printStackTrace();
        }
    }

    /**
     * 启动主类、并清除task中主类之上的Activity
     */
    public static void startLauncherActivity(Activity context) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 启动主类、并清除task中主类之上的Activity
     */
    public static void startMainActivityClearTop(Activity context) {
        Intent intent = new Intent();
        intent.setClass(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("clearTop", true);
        context.startActivity(intent);
    }

    /**
     * 判断客户端是否安装了百度地图
     *
     * @param context
     * @return
     */
    public static boolean isInstallBaiduMap(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        if (packageInfos != null) {
            for (PackageInfo packageInfo : packageInfos) {
                if (packageInfo.packageName.equals("com.baidu.BaiduMap")) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断客户端是否安装了高德地图
     *
     * @param context
     * @return
     */
    public static boolean isInstallGaodeMap(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        if (packageInfos != null) {
            for (PackageInfo packageInfo : packageInfos) {
                if (packageInfo.packageName.equals("com.autonavi.minimap")) {
                    return true;
                }
            }
        }
        return false;
    }
}
